package com.netty.web;

import com.alibaba.fastjson.JSON;
import com.netty.model.SentBody;
import com.netty.service.DefaultMessagePusher;
import com.netty.utils.R;
import com.netty.vo.MessageEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Api(tags = "消息推送")
@Slf4j
@RestController
@RequestMapping("/message")
public class TMessageController {
    @Autowired
    private DefaultMessagePusher defaultMessagePusher;

    @ApiOperation(value = "一对一发送消息")
    @PostMapping(value = "/send")
    public R send(@Validated @RequestBody MessageEntity.P2PEntity messageEntity) {
        try {
            String uid = messageEntity.getToId()+"";
            SentBody sentBody = new SentBody();
            sentBody.setTimestamp(new Date().getTime());
            sentBody.setKey("user");
            sentBody.put("body", messageEntity.getMsg());
            sentBody.put("fromId", messageEntity.getFromId()+"");
            defaultMessagePusher.push(uid,sentBody);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    @ApiOperation(value = "发送给所有在线用户")
    @PostMapping(value = "/sendAll")
    public R sendAll(@Validated @RequestBody MessageEntity.P2ALLEntity messageEntity) {
        try {
            SentBody sentBody = new SentBody();
            sentBody.setKey("user");
            sentBody.setTimestamp(new Date().getTime());
            sentBody.put("body", JSON.toJSONString(messageEntity));
            defaultMessagePusher.pushAll(sentBody);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    @ApiOperation(value = "一对多发送消息")
    @PostMapping(value = "/sendGroup")
    public R sendGroup(@Validated @RequestBody MessageEntity.P2MEntity messageEntity){
        try {
            if(Objects.nonNull(messageEntity.getGroupId())){
                SentBody sentBody = new SentBody();
                sentBody.setKey("user");
                sentBody.setTimestamp(new Date().getTime());
                sentBody.put("body", messageEntity.getMsg());
                defaultMessagePusher.push2Group(messageEntity.getGroupId(),sentBody);
                return R.ok();
            }
            return R.error("分组id不能为空");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    @ApiOperation(value = "加入分组")
    @PostMapping("/addGroup")
    public R addGroup(@RequestParam("userId") String userId,@RequestParam("groupId") String groupId){
        try{
            defaultMessagePusher.addGroup(userId,groupId);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error("加入分组失败");
        }
    }

    @ApiOperation(value = "获取当前在线用户")
    @GetMapping("/queryOnlineUser")
    public R queryOnlineUser(){
        try{
            List<Map<String, Object>> maps = defaultMessagePusher.queryOnlineUser();
            return R.ok().put("data",maps);
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }
    @ApiOperation(value = "获取分组当前用户")
    @GetMapping("/queryGroupOnlineUser")
    public R queryGroupOnlineUser(@RequestParam("groupId") String groupId){
        try{
            List<Map<String, Object>> maps = defaultMessagePusher.queryGroupOnlineUser(groupId);
            return R.ok().put("data",maps);
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }


}
